<?php
	class postClass
	{
		var $pid,$name,$time,$group,$owner,$content,$status;
		
		public function __construct($data = array())
		{
			if (isset($data['pid'])) $this->pid = $data['pid'];
			$this->name = $data['postName'];
			$this->time = $data['postTime'];
			$this->group = $data['postGroup'];
			$this->owner = $data['postOwner'];
			$this->content = $data['postContent'];
			$this->status = $data['postStatus'];
		}
		
		public function create()
		{
			$conn = new mysqlClass();
			$this->pid = $this->getNewPid();
			$query = "INSERT INTO posts (pid,postName,postTime,postGroup,postOwner,postContent,postStatus) VALUES ({$this->pid},'{$this->name}','{$this->time}','{$this->group}',{$this->owner},'{$this->content}','{$this->status}');";
			if ($conn->query($query))
			{
				$query = "UPDATE record SET value={$this->pid} WHERE name='postAmount';";
				return $conn->query($query);
			}
			else
			{
				return FALSE;
			}
		}

		public static function getPostByPid($pid)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM posts WHERE pid={$pid};";
			$conn->query($query);
			if ($conn->row != 0)
			{
				$conn->fetch();
				return new postClass($conn->data);
			}
			else
			{
				return "NO";
			}
		}
		
		public static function getPostByGroup($group)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM posts WHERE postGroup='{$group}';";
			$conn->query($query);
			if ($conn->row == 0)
			{
				return 'NO';
			}
			else
			{
				return $conn->result;
			}
		}
		
		public static function getReplyByPid($pid)
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM posts WHERE postStatus='reply-{$pid}' ORDER BY postTime;";
			$conn->query($query);
			if ($conn->row == 0)
			{
				return 'NO';
			}
			else
			{
				return $conn->result;
			}
		}
		
		private function getNewPid()
		{
			$conn = new mysqlClass();
			$query = "SELECT * FROM record WHERE name='postAmount';";
			$conn->query($query);
			$conn->fetch();
			return $conn->data['value'] + 1;
		}
	}